package com.bosssoft.boss_exam_core.util.web;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @desc 应用级对象获取工具类
 */
public class RequestContextUtil {

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/13
     * @Description 获取request
     * @Param []
     * @Return javax.servlet.http.HttpServletRequest
     * @Exception //无
     * @since JDK 1.8       
     **/
    public static HttpServletRequest getRequest() {
       return getRequestAttributes().getRequest();
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/13
     * @Description 获取response
     * @Param []
     * @Return javax.servlet.http.HttpServletResponse
     * @Exception 无     
     * @since JDK 1.8       
     **/
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/13
     * @Description 获取session
     * @Param []
     * @Return javax.servlet.http.HttpSession
     * @Exception 无     
     * @since JDK 1.8       
     **/
    public static HttpSession getSession() {
        return getRequest().getSession();
    }
    
    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/13
     * @Description 获取ServletRequestAttributes
     * @Param []
     * @Return org.springframework.web.context.request.ServletRequestAttributes
     * @Exception 无     
     * @since JDK 1.8       
     **/
    public static ServletRequestAttributes getRequestAttributes() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/13
     * @Description 获取ServletContext
     * @Param []
     * @Return javax.servlet.ServletContext
     * @Exception 无     
     * @since JDK 1.8       
     **/
    public static ServletContext getServletContext() {
        return ContextLoader.getCurrentWebApplicationContext().getServletContext();
    }

}
